<template>
  <div class="login-container">
    <!-- 头部 -->
    <van-nav-bar title="登录">
      <template #left>
        <img class="logo"
             src="@/assets/images/logo.png" />
      </template>
    </van-nav-bar>
    <!-- 表单 -->
    <van-form @submit="onSubmit">
      <!-- 手机号 -->
      <van-field v-model="formLogin.mobile"
                 type="tel"
                 label="手机号"
                 placeholder="手机号"
                 required
                 :rules="rules.tel" />
      <!-- 密码 -->
      <van-field v-model="formLogin.code"
                 type="password"
                 label="密码"
                 placeholder="密码"
                 required
                 :rules="rules.password" />
      <!-- 登录按钮 -->
      <div style="margin: 16px;">
        <van-button block
                    :loading='isLoading'
                    loading-text="加载中..."
                    type="info"
                    round
                    native-type="submit">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { Notify } from 'vant'
import { mapActions } from 'vuex'
export default {
  name: 'Login',
  data () {
    return {
      rules: {
        tel: [
          {
            required: true,
            message: '请填写手机号',
            trigger: 'onChange',
            pattern: /^1[3456789]\d{9}$/
          }
        ],
        password: [
          {
            required: true,
            message: '请填写密码',
            trigger: 'onChange',
            pattern: /^\d{6}$/
          }
        ]
      },
      isLoading: false,
      // 登录表单数据
      formLogin: {
        // 手机号
        mobile: '13888888888',
        // 验证码
        code: '246810' // 默认只能用246810后台规定了
      }
    }
  },
  methods: {
    ...mapActions(['asyncLoginAction']),
    async onSubmit () {
      this.isLoading = true
      try {
        await this.asyncLoginAction({ tel: this.formLogin.mobile, pwd: this.formLogin.code })
        Notify({ type: 'success', message: '登录成功' })
      } catch (err) {
        console.log(err)
        Notify({ type: 'danger', message: '手机号或验证码不正确' })
      } finally {
        // 不管成功与失败，都要将按钮解禁
        this.isLoading = false
      }
    }

  },
  created () {

  }
}
</script>

<style lang="less" scoped>
.logo {
  width: 100px;
  height: 30px;
}
// .van-nav-bar {
//   background: #007bff;
// }

// /deep/.van-nav-bar__title {
//   color: white;
// }
</style>
